$(document).ready(function () {
    init();
    queryConditions();

})


//初始化
var init=function(){
    $.ajax({
        url: "http://localhost:8080/partyActivity/queryPartyNameAndPON",
        type: "GET",
        async: false,
        data:{"partyMemberAccount":"20151000"},
        dataType: "json",
        contentType:"application/json",
        success:function (data) {
            console.log(data);
            console.log(data.data);
            var partyName=data.data.partyName;
            var PON=data.data.PON;
            console.log("partyName="+partyName);
            console.log("PON="+PON);
            $('#dataTable').dataTable({
                "bDeferRender": true,
                "sAjaxDataProp": "data.partyActivityList",
                "sAjaxSource": "http://localhost:8080/partyActivity/queryPartyActivityByConditionsByCache?partyOrganizationNum="+PON+"&partyActivityName=&partyActivityStartTime=&partyActivityEndTime=",
                "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
                "fnPreDrawCallback": function (oSettings) {
                    $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
                    $('.dataTables_length select').addClass('form-control input-small');
                },
                "oLanguage": {
                    "sSearch": "",
                    "sLengthMenu": "<span>_MENU_ entries</span>",
                    "sZeroRecords": "暂无相关信息",
                },
                "bJQueryUI": false,
                "bAutoWidth": false,
                "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",

                "aoColumnDefs": [
                    {
                        "mDataProp": null,
                        "sDefaultContent": partyName,
                        "aTargets": [-2]
                    }
                ],
                "aoColumns": [
                    {"mDataProp":"partyActivityId"},
                    {"mDataProp":"partyActivityName"},
                    {"mDataProp":"partyActivityTime"},
                    {"mDataProp":""},
                    {
                        "mDataProp": "partyActivityDescription",
                        "mRender": function (data) {
                            return html_decode(data);
                        }
                    },
                ],
            })
        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });
}


//动态查询
var queryConditions=function(){
    $(document).on('click', '#queryConditions', function () {
        var $partyActivityName = $("#partyActivityName").val();
        var $partyActivityStartTime;
        var $partyActivityEndTime;
        var pst=getDate($("#partyActivityStartTime").val());
        var pet=getDate($("#partyActivityEndTime").val());
        console.log("pst="+pst);
        console.log("pet="+pet);
        if(pst=='undefined-undefined-'){
            $partyActivityStartTime=null;
        }else {
            $partyActivityStartTime=pst;
        }

        if(pet=='undefined-undefined-'){
            $partyActivityEndTime=null;
        }else {
            $partyActivityEndTime=pet;
        }

        console.log("$partyActivityStartTime="+$partyActivityStartTime);
        console.log("$partyActivityEndTime="+$partyActivityEndTime);


        $.ajax({
            url: "http://localhost:8080/partyActivity/queryPartyNameAndPON",
            type: "GET",
            async: false,
            data:{"partyMemberAccount":"20151000"},
            dataType: "json",
            contentType:"application/json",
            success:function (data) {
                console.log(data);
                console.log(data.data);
                var partyName=data.data.partyName;
                var PON=data.data.PON;
                console.log("partyName="+partyName);
                console.log("PON="+PON);
                console.log("http://localhost:8080/partyActivity/queryPartyActivityByConditionsByCache?" +
                    "partyOrganizationNum="+PON+"&partyActivityName="+$partyActivityName+"&partyActivityStartTime="+$partyActivityStartTime+"&partyActivityEndTime="+$partyActivityEndTime);
                //datatable不能二次初始化，如果不先销毁原datatable，会报错
                var table = $('#dataTable').dataTable()
                if (table) {
                    table.fnDestroy();
                }
                $('#dataTable').dataTable({
                    "bDeferRender": true,
                    "sAjaxDataProp": "data.partyActivityList",
                    "sAjaxSource": "http://localhost:8080/partyActivity/queryPartyActivityByConditions?" +
                    "partyOrganizationNum="+PON+"&partyActivityName="+$partyActivityName+"&partyActivityStartTime="+$partyActivityStartTime+"&partyActivityEndTime="+$partyActivityEndTime,
                    "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
                    "fnPreDrawCallback": function (oSettings) {
                        $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
                        $('.dataTables_length select').addClass('form-control input-small');
                    },
                    "oLanguage": {
                        "sSearch": "",
                        "sLengthMenu": "<span>_MENU_ entries</span>",
                        "sZeroRecords": "暂无相关信息",
                    },
                    "bJQueryUI": false,
                    "bAutoWidth": false,
                    "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",

                    "aoColumnDefs": [
                        {
                            "mDataProp": null,
                            "sDefaultContent": partyName,
                            "aTargets": [-2]
                        }
                    ],
                    "aoColumns": [
                        {"mDataProp":"partyActivityId"},
                        {"mDataProp":"partyActivityName"},
                        {"mDataProp":"partyActivityTime"},
                        {"mDataProp":""},
                        {
                            "mDataProp": "partyActivityDescription",
                            "mRender": function (data) {
                                return html_decode(data);
                            }
                        },
                    ],
                })
            },
            error:function () {
                layer.msg("网络在开小差~稍后重试");
            }
        });


    });
}


function getUrlParms(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)
        return unescape(r[2]);
    return null;
}

function getDate(str) {
    if(str.indexOf("-") >= 0 ){
        return str;
    }else {
        var arr=str.split(" ");
        console.log(arr[1]);
        var day=arr[0];
        var month=arr[1];
        var year=arr[2];
        if(month=='January'){
            month="01";
        }else if(month=='February'){
            month="02";
        }else if(month=='March'){
            month="03";
        }else if(month=='April'){
            month="04";
        }else if(month=='May'){
            month="05";
        }else if(month=='June'){
            month="06";
        }else if(month=='July'){
            month="07";
        }else if(month=='August'){
            month="08";
        }else if(month=='September'){
            month="09";
        }else if(month=='October'){
            month="10";
        }else if(month=='November'){
            month="11";
        }else if(month=='December'){
            month="12";
        }
        return year+"-"+month+"-"+day;
    }
}

function html_decode(str) {
    var s = "";
    if (str.length == 0)
        return "";
    s = str.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    s = s.replace(/<br>/g, "\n");
    return s;
}